$(function() {
	$("#form").validate({
		submitHandler : function(form) {
			var length = $('input:checkbox[class=needCkCls]:checked').length;
			var length2 = $('input:checkbox[class=needCkCls2]:checked').length;
			//alert(length);alert(length2);
			if(length==0||length2==0){
				var message = null;
				if(length==0){message = "定价";}
				if(length2==0){message = "选择角色";}
				var info = "您尚未"+message+",请完成再保存！"
				layer.alert(info);
			}else{
				form.submit();
			}
		},
		rules : {
			"no" : {
				required : true,
				remote : { // 异步验证是否存在
					type : "POST",
					url : 'management/serviceitem/isExist',
					data : {
						no : function() {
							return $("#no").val();
						},
						serviceItemId : function() {
							return $("#id").val();
						}
					}
				}
			},
			"name" : {
				required : true,
				remote : { // 异步验证是否存在
					type : "POST",
					url : 'management/serviceitem/isExist',
					data : {
						name : function() {
							return $("#name").val();
						},
						serviceItemId : function() {
							return $("#id").val();
						}
					}
				}
			},
			"itemCategoryId" : {
				required : true
			}
		},
		messages : {
			"no" : {
				required : "请输入项目编号",
				remote : "该项目编号已经存在"
			},
			"name" : {
				required : "请输入项目名称",
				remote : "该项目名称已经存在"
			},
			"itemCategoryId" : {
				required : "请选择项目类别"
			}
		}
	});
	
	//check事件
	$(".needCkCls,.needCkCls2").click(function() {
        if(this.checked){
        	$(this).siblings(":input").prop("disabled",false);
        	$(this).parent().siblings().find(":input").prop("disabled",false);
        }else{
        	$(this).siblings(":input").prop("disabled",true);
        	$(this).parent().siblings().find(":input").prop("disabled",true);
        }
    });
	
	//抽成方式选择事件
	$(".commisionSelCls").change(function () {
		var val = $(this).val();
		var inputObj = $(this).parent().next().children().first();
		var oldName = inputObj.attr("name");
		var index = oldName.indexOf(".");
		if(val=='amount'){
			var name = oldName.substring(0,index+1)+"commisionAmount";
			inputObj.removeClass("isRatio");
			inputObj.addClass("isPriceOne");
			inputObj.attr("name",name);
			inputObj.attr("placeholder","请输入抽成金额");
		}else{
			var name = oldName.substring(0,index+1)+"commisionRatio";
			inputObj.removeClass("isPriceOne");
			inputObj.addClass("isRatio");
			inputObj.attr("name",name);
			inputObj.attr("placeholder","请输入抽成比例（百分比%）");
		}
		inputObj.val("");
	});
	
	/**
	 * 如果是详情页面，禁用所有表单元素，只能查看
	 */
	if(window.location.href.indexOf("detail")>0){
		$(":input").attr("disabled","true");
	}
});